<template>
  <div style=" background: #5a78ec;">
    <!-- navbar -->
    <!-- <van-nav-bar title="发布" @click-left="onClickLeft" fixed>
      <template #left>
        <van-icon name="arrow-left" size="18" />
      </template>
      <template #right>
        <van-icon name="ellipsis" size="18" />
      </template>
    </van-nav-bar> -->
    <div class="contanier">
      <!-- <div class="box_cat"></div> -->
      <div class="form">
        <!-- 封面 -->
        <van-row style="padding-top:40px;">
          <van-col span="7">封面：</van-col>
          <van-col span="8" class="bookface"
            ><van-uploader
              v-model="fileList"
              multiple
              :max-count="1"
              :max-size="500 * 1024"
              :after-read="afterRead"
              ref="file"
          /></van-col>
          <van-col span="8" style="text-align: left;">
            <div class="comuicate">
              <img src="../../assets/global/img/comuicate.png" alt="" />
            </div>
          </van-col>
        </van-row>
        <!-- 书名 -->
        <van-row>
          <van-col span="7">书名：</van-col>
          <van-col span="16">
            <input
              type="text"
              placeholder="请输入书名"
              v-model="publishData.bookName"
              required
            />
            <input type="hidden" name="action_e" value="Add_New" />
          </van-col>
        </van-row>
        <!-- 作者 -->
        <van-row>
          <van-col span="7">作者：</van-col>
          <van-col span="16">
            <input
              type="text"
              placeholder="请输入作者名"
              v-model="publishData.author"
            />
          </van-col>
        </van-row>
        <!-- 简介 -->
        <van-row>
          <van-col span="7">简介：</van-col>
          <van-col span="16">
            <textarea
              cols="20"
              rows="5"
              placeholder="请输入内容"
              v-model="publishData.details"
            ></textarea>
          </van-col>
        </van-row>

        <!-- 电话 -->
        <van-row>
          <van-col span="7">电话：</van-col>
          <van-col span="16">
            <input
              type="number"
              placeholder="请输入联系电话"
              v-model="publishData.phone"
              id="phone"
              max="11"
              @blur="animate()"
            />
          </van-col>
        </van-row>
        <!-- 时间 -->
        <van-row>
          <van-col span="7">到期时间：</van-col>
          <van-col span="16">
            <input
              type="number"
              placeholder="请输入借阅天数"
              v-model="publishData.lendTime"
            />
          </van-col>
        </van-row>
        <!-- 地址 -->
        <van-row>
          <van-col span="7">取书地址：</van-col>
          <van-col span="16">
            <div class="area_box">
              <van-field
                readonly
                clickable
                name="area"
                v-model="value"
                placeholder="点击选择省市区"
                @click="showArea = true"
                @change="addressChange"
              />
              <van-popup v-model="showArea" position="bottom">
                <van-area
                  title="省市区"
                  :area-list="areaList"
                  @confirm="onConfirm"
                  @cancel="showArea = false"
                  value="110101"
                />
              </van-popup>
            </div>
          </van-col>
        </van-row>
        <van-row>
          <van-col span="7"></van-col>
          <van-col span="16">
            <textarea
              cols="15"
              rows="5"
              placeholder="请输入详细地址"
              v-model="publishData.address"
              >{{ publishData.address }}</textarea
            >
          </van-col>
        </van-row>
        <!-- 按钮 -->
        <van-row gutter="20">
          <van-col span="8"></van-col>
          <van-col span="8"
            ><van-button round type="info" @click="getPublishBook"
              >发布</van-button
            ></van-col
          >
          <van-col span="8"></van-col>
        </van-row>
      </div>
    </div>
  </div>
</template>

<script>
import { Dialog, Toast } from "vant";
import { areaList } from "../Publish/area.js";

// 埋点
// 单页应用 或 “单个页面”需异步补充 PV 日志参数还需进行如下埋点：
aplus_queue.push({
  action: "aplus.setMetaInfo",
  arguments: ["aplus-waiting", "MAN"],
});
// 单页应用路由切换后 或 在异步获取到 pv 日志所需的参数后再执行 sendPV：
aplus_queue.push({
  action: "aplus.sendPV",
  arguments: [
    { is_auto: false },
    {
      // 自定义 PV 参数 key-value 键值对（只能是这种平铺的 json，不能做多层 嵌套），如：
      miniAppId: "2001886320",
    },
  ],
});

export default {
  data() {
    return {
      value: "",
      showArea: false,
      areaList, // 数据格式见 Area 组件文档
      fileList: [], //图片展示i
      // disabled: true, //初始为禁用状态

      // 发布参数
      publishData: {
        address: "",
        author: "",
        bookName: "",
        details: "",
        image: "",
        lendTime: "",
        owner: "",
        phone: "",
        imgUrl: "",
      },
      content: "",
      banner: "",
      count: "", //倒计时
    };
  },
  methods: {
    handleConfirm(e) {
      console.log(e);
    },
    //地址下拉选项改变
    addressChange(e) {
      console.log(e);
    },
    checkMessage() {},
    //mgop 发布 owner
    async getPublishBook() {
      if (
        this.publishData.address !== "" &&
        this.publishData.author !== "" &&
        this.publishData.bookName !== "" &&
        this.publishData.details !== "" &&
        this.fileList !== "" &&
        this.publishData.lendTime !== "" &&
        this.publishData.owner != "" &&
        this.publishData.phone !== ""
      ) {
        // this.disabled = false; //非禁用
        // const res = await this.http.Post("/info/add", {
        const res = await this.mgopfn("mgop.educational.system.add", {
          address: this.publishData.address,
          author: this.publishData.author,
          bookName: this.publishData.bookName,
          details: this.publishData.details,
          image: this.imgUrl,
          lendTime: this.publishData.lendTime,
          owner: this.publishData.owner,
          phone: this.publishData.phone,
        });
        console.log(res);
        Toast("发布成功");
        // 3秒后跳转首页
        const TIME_COUNT = 300;
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
              //
              this.$route.push("/home");
            }
          });
        }
      } else {
        Toast("请填写全部信息");
      }
    },
    // setTimeout ('test(参数)',1000)；
    // 非本地用户----警告
    publish() {
      Dialog.alert({
        message: "您未成为书城用户，不可使用发布功能",
        theme: "round-button",
        confirmButtonColor: "#5a78ec",
      }).then(() => {
        // on close
      });
    },
    // 图片上传
    async afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.content = file.content;
      console.log(this.content);

      // let imgbase = fileList[0].content.replace(/^data:image\/\w+;base64,/, "");
      // var imgbase = file.content;
      // var param = new FormData();
      // param.append("filedata", 123);
      // const res = await this.http.upload("/upload", {
      const res = await this.mgopfn("mgop.educational.system.upload", {
        filedata: this.content,
      });
      console.log(res);
      Toast("图片上传成功");
      this.imgUrl = res.data.data;
      // this.imgUrl = res.data;
      console.log("imgurl:" + this.imgUrl);
    },
    // async getPic() {
    //   let param = new FormData();
    //   param.append("file", this.fileContent);
    //   const res = await this.mgopfn("mgop.educational.system.upload", param);
    //   console.log(res);
    //   this.imgUrl = res;
    //   console.log("imgurl" + this.imgUrl);
    // },
    beforeRead(file) {
      console.log(file);
      return true;
    },
    beforeDelete(file) {
      console.log(file);
      return false;
    },
    uploadImg(file) {
      console.log(file); //控制台可以看见图片信息
    },
    // 电话号码验证
    animate() {
      var theinput = document.getElementById("phone").value;
      var p1 = /^(13[0-9]\d{8}|15[0-35-9]\d{8}|18[0-9]\{8}|14[57]\d{8})$/;
      //(p1.test(theinput));
      if (p1.test(theinput) == false) {
        Toast("请填写正确电话号码!!");
        document.getElementById("phone").value = "";
      } else Toast("电话号码正确");
    },
    // area地区
    Area() {
      this.$router.push("/area");
    },
    onConfirm(values) {
      console.log(values);

      this.publishData.address = values
        .filter((item) => !!item)
        .map((item) => item.name)
        .join("");
      this.value = this.publishData.address;
      this.showArea = false;
    },
    //
  },
  mounted() {
    this.publishData.owner = window.localStorage.getItem("usernameData");
    console.log("owner:" + this.publishData.owner);
    console.log("owner本地" + window.localStorage.getItem("usernameData"));
    // this.getPublishBook();
    // this.getPic();
  },
  //
  watch: {},
};
</script>

<style scoped>
input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {
  /* WebKit browsers */
  color: #d1d4d6;
}
input:-moz-placeholder,
textarea::-webkit-input-placeholder {
  /* Mozilla Firefox 4 to 18 */
  color: #d1d4d6;
}
input::-moz-placeholder,
textarea::-webkit-input-placeholder {
  /* Mozilla Firefox 19+ */
  color: #d1d4d6;
}
input:-ms-input-placeholder,
textarea::-webkit-input-placeholder {
  /* Internet Explorer 10+ */
  color: #d1d4d6;
}

@media screen and (min-width: 320px) {
  .van-nav-bar {
    width: 100%;
    line-height: 22px;
    text-align: center;
  }
  /* pic */
  .comuicate {
    width: 80px;
    height: 80px;
    /* border: 1px solid red; */
    /* background: pink; */
  }
  .comuicate img {
    width: 100%;
    height: 100%;
  }
  /deep/.van-uploader__upload {
    border: 1px dashed #000000;
  }
  /deep/.van-uploader__upload-icon {
    font-size: 28px;
  }
  /deep/.van-uploader__preview-image {
    width: 80px;
    height: 80px;
  }
  /deep/.van-uploader__preview-delete {
    width: 20px;
    height: 20px;
  }
  /deep/.van-uploader__preview-delete-icon {
    top: -2px;
    right: -2px;
    color: #fff;
    font-size: 22px;
  }
  .contanier {
    width: 100%;
    height: 100%;
    background: #5a78ec;
    padding-top: 5px;
    padding-bottom: 40px;
    margin-bottom: 50px;
  }
  .form {
    display: block;
    border-radius: 10px;
    width: 90%;
    height: 100%;
    background: #fff;
    margin: 0 auto;
    margin-top: 10px;
    padding-bottom: 20px;
  }
  .van-col {
    font-size: 12px;
    /* color: rgba(80, 80, 80, 1); */
    text-align: right;
  }
  input {
    width: 80%;
    height: 30px;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 12px;
  }
  textarea {
    width: 80%;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 5px 10px;
    font-size: 12px;
  }
  .van-row {
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .van-button {
    width: 100%;
    height: 28px;
    background: #5a78ec;
    border-radius: 10px;
    font-size: 12px;
  }
  .box_cat {
    width: 100%;
    height: 40px;
    /* background: pink; */
  }

  /deep/.van-uploader {
    float: left;
    /* border: 1px solid red; */
  }
  select {
    width: 90%;
    height: 30px;
    padding: 0 10px;
    display: block;
  }
  option {
    width: 90%;
    height: 30px;
  }
  .area_box {
    width: 80%;
    height: 30px;
    padding: 3px 10px;
    display: block;
    border: 1px solid #333;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 12px;
  }
  /deep/.area_box .van-cell {
    padding: 0;
  }
  /deep/.van-field__control {
    padding-top: 3px;
    font-size: 12px;
  }
  /deep/.van-dialog .van-button__content {
    background: #5a78ec;
  }
}
@media screen and (min-width: 375px) {
  .van-nav-bar {
    width: 100%;
    line-height: 22px;
    text-align: center;
  }
  /* pic */
  .comuicate {
    width: 80px;
    height: 80px;
    /* border: 1px solid red; */
    /* background: pink; */
  }
  .comuicate img {
    width: 100%;
    height: 100%;
  }
  /deep/.van-uploader__upload {
    border: 1px dashed #999999;
  }
  /deep/.van-uploader__upload-icon {
    font-size: 28px;
  }
  /deep/.van-uploader__preview-image {
    width: 80px;
    height: 80px;
  }
  /deep/.van-uploader__preview-delete {
    width: 20px;
    height: 20px;
  }
  /deep/.van-uploader__preview-delete-icon {
    top: -2px;
    right: -2px;
    color: #fff;
    font-size: 22px;
  }
  .contanier {
    width: 100%;
    height: 100%;
    background: #5a78ec;
    padding-top: 5px;
    padding-bottom: 40px;
    margin-bottom: 50px;
  }
  .form {
    display: block;
    border-radius: 10px;
    width: 90%;
    height: 100%;
    background: #fff;
    margin: 0 auto;
    margin-top: 10px;
    padding-bottom: 20px;
  }
  .van-col {
    font-size: 14px;
    /* color: rgba(80, 80, 80, 1); */
    text-align: right;
  }
  input {
    width: 80%;
    height: 30px;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 12px;
  }
  textarea {
    width: 80%;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 5px 10px;
    font-size: 12px;
  }
  .van-row {
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .van-button {
    width: 100%;
    height: 28px;
    background: #5a78ec;
    border-radius: 10px;
    font-size: 12px;
  }
  .box_cat {
    width: 100%;
    height: 40px;
    /* background: pink; */
  }

  /deep/.van-uploader {
    float: left;
    /* border: 1px solid red; */
  }
  select {
    width: 90%;
    height: 30px;
    padding: 0 10px;
    display: block;
  }
  option {
    width: 90%;
    height: 30px;
  }
  .area_box {
    width: 80%;
    height: 30px;
    padding: 3px 10px;
    display: block;
    border: 1px solid #333;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 12px;
  }
  /deep/.area_box .van-cell {
    padding: 0;
  }
  /deep/.van-field__control {
    font-size: 12px;
    /* color: rgba(80, 80, 80, 1); */
  }
}
@media screen and (min-width: 414px) {
  .van-nav-bar {
    width: 100%;
    line-height: 22px;
    text-align: center;
  }
  /* pic */
  .comuicate {
    width: 80px;
    height: 80px;
    /* border: 1px solid red; */
    /* background: pink; */
  }
  .comuicate img {
    width: 100%;
    height: 100%;
  }
  /deep/.van-uploader__upload {
    border: 1px dashed #000000;
  }
  /deep/.van-uploader__upload-icon {
    font-size: 28px;
  }
  /deep/.van-uploader__preview-image {
    width: 80px;
    height: 80px;
  }
  /deep/.van-uploader__preview-delete {
    width: 20px;
    height: 20px;
  }
  /deep/.van-uploader__preview-delete-icon {
    top: -2px;
    right: -2px;
    color: #fff;
    font-size: 22px;
  }
  .contanier {
    width: 100%;
    height: 100%;
    background: #5a78ec;
    padding-top: 5px;
    padding-bottom: 40px;
    margin-bottom: 50px;
  }
  .form {
    display: block;
    border-radius: 10px;
    width: 90%;
    height: 100%;
    background: #fff;
    margin: 0 auto;
    margin-top: 10px;
    padding-bottom: 20px;
  }
  .van-col {
    font-size: 16px;
    /* color: rgba(80, 80, 80, 1); */
    text-align: right;
  }
  input {
    width: 80%;
    height: 30px;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 14px;
  }
  textarea {
    width: 80%;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 5px 10px;
    font-size: 14px;
  }
  .van-row {
    margin-top: 20px;
    margin-bottom: 20px;
  }
  .van-button {
    width: 100%;
    height: 28px;
    background: #5a78ec;
    border-radius: 10px;
    font-size: 14px;
  }
  .box_cat {
    width: 100%;
    height: 40px;
    /* background: pink; */
  }

  /deep/.van-uploader {
    float: left;
    /* border: 1px solid red; */
  }
  select {
    width: 90%;
    height: 30px;
    padding: 0 10px;
    display: block;
  }
  option {
    width: 90%;
    height: 30px;
  }
  .area_box {
    width: 80%;
    height: 30px;
    padding: 3px 10px;
    display: block;
    border: 1px solid #333;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 12px;
  }
  /deep/.area_box .van-cell {
    padding: 0;
  }
  /deep/.van-field__control {
    padding-top: 2px;
    font-size: 14px;
    /* color: rgba(80, 80, 80, 1); */
  }
}
@media screen and (min-width: 768px) {
  /deep/.van-nav-bar {
    width: 100%;
    line-height: 22px;
    font-size: 22px;
  }
  /deep/.van-nav-bar__content {
    height: 60px !important;
  }
  /deep/.van-nav-bar__title {
    font-size: 24px !important;
  }
  /* pic */
  .comuicate {
    width: 150px;
    height: 150px;
    /* border: 1px solid red; */
    /* background: pink; */
  }
  .comuicate img {
    width: 100%;
    height: 100%;
  }
  /deep/.van-uploader__upload {
    border: 2px dashed #999999;
    width: 150px;
    height: 150px;
  }
  /deep/.van-uploader__upload-icon {
    font-size: 40px;
  }
  /deep/.van-uploader__preview-image {
    width: 150px;
    height: 150px;
  }
  /deep/.van-uploader__preview-delete {
    width: 30px;
    height: 30px;
  }
  /deep/.van-uploader__preview-delete-icon {
    top: -2px;
    right: -2px;
    color: #fff;
    font-size: 30px;
  }
  .contanier {
    width: 100%;
    height: 100%;
    background: #5a78ec;
    padding-top: 5px;
    padding-bottom: 50px;
    margin-bottom: 100px;
  }
  .form {
    display: block;
    border-radius: 10px;
    width: 90%;
    height: 100%;
    background: #fff;
    margin: 0 auto;
    margin-top: 10px;
    padding-bottom: 20px;
  }
  .van-col {
    font-size: 20px;
    /* color: rgba(80, 80, 80, 1); */
    text-align: right;
  }
  input {
    width: 80%;
    height: 40px;
    line-height: 40px;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 18px;
  }
  textarea {
    width: 80%;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 5px 10px;
    font-size: 18px;
  }
  .van-row {
    margin-top: 30px;
    margin-bottom: 30px;
  }
  .van-button {
    width: 100%;
    height: 40px;
    background: #5a78ec;
    border-radius: 10px;
    font-size: 18px;
    margin-top: 10px;
  }
  .box_cat {
    width: 100%;
    height: 40px;
    /* background: pink; */
  }

  /deep/.van-uploader {
    float: left;
    /* border: 1px solid red; */
  }
  select {
    width: 90%;
    height: 30px;
    padding: 0 10px;
    display: block;
  }
  option {
    width: 90%;
    height: 30px;
  }
  .area_box {
    width: 80%;
    height: 30px;
    padding: 3px 10px;
    display: block;
    border: 1px solid #333;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 7px 10px;
    font-size: 12px;
  }
  /deep/.area_box .van-cell {
    padding: 0;
  }
  /deep/.van-field__control {
    padding-top: 2px;
    font-size: 18px;
    /* color: rgba(80, 80, 80, 1) !important; */
  }
  /deep/.van-dialog {
    width: 500px;
    height: 400px;
  }
  /deep/.van-dialog__message {
    font-size: 32px;
  }
  /deep/.van-dialog--round-button .van-dialog__cancel,
  /deep/.van-dialog--round-button .van-dialog__confirm {
    height: 200px;
  }
  /deep/.van-dialog--round-button .van-dialog__confirm {
    color: #fff;
    font-size: 18px;
  }
}
@media screen and (min-width: 1024px) {
  /deep/.van-nav-bar {
    width: 100%;
    line-height: 22px;
    font-size: 26px;
  }
  /deep/.van-nav-bar__content {
    height: 80px !important;
  }
  /deep/.van-nav-bar__title {
    font-size: 26px !important;
  }
  .comuicate {
    width: 200px;
    height: 200px;
    /* border: 1px solid red; */
    /* background: pink; */
  }
  .comuicate img {
    width: 100%;
    height: 100%;
  }
  /deep/.van-uploader__upload {
    border: 4px dashed #999999;
    width: 200px;
    height: 200px;
  }
  /deep/.van-uploader__upload-icon {
    font-size: 50px;
  }
  /deep/.van-uploader__preview-image {
    width: 200px;
    height: 200px;
  }
  /deep/.van-uploader__preview-delete {
    width: 40px;
    height: 40px;
  }
  /deep/.van-uploader__preview-delete-icon {
    top: 5px;
    right: 5px;
    color: #fff;
    font-size: 30px;
  }
  .contanier {
    width: 100%;
    height: 100%;
    background: #5a78ec;
    padding-top: 5px;
    padding-bottom: 80px;
    margin-bottom: 100px;
  }
  .form {
    display: block;
    border-radius: 10px;
    width: 90%;
    height: 100%;
    background: #fff;
    margin: 0 auto;
    margin-top: 10px;
    padding-bottom: 50px;
  }
  .van-col {
    font-size: 24px;
    /* color: rgba(80, 80, 80, 1); */
    text-align: right;
  }
  input {
    width: 80%;
    height: 50px;
    line-height: 50px;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 0 10px;
    font-size: 22px;
  }
  textarea {
    width: 80%;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 5px 10px;
    font-size: 22px;
  }
  .van-row {
    margin-top: 50px;
    margin-bottom: 50px;
  }
  .van-button {
    width: 100%;
    height: 50px;
    background: #5a78ec;
    border-radius: 10px;
    font-size: 22px;
    margin-top: 10px;
  }
  .box_cat {
    width: 100%;
    height: 60px;
    /* background: pink; */
  }

  /deep/.van-uploader {
    float: left;

    /* border: 1px solid red; */
  }
  .van-uploader__upload {
    width: 100px;
    height: 100px;
  }
  select {
    width: 90%;
    height: 30px;
    padding: 0 10px;
    display: block;
  }
  option {
    width: 90%;
    height: 30px;
  }
  .area_box {
    width: 80%;
    height: 50px;
    padding: 3px 10px;
    display: block;
    border: 1px solid #333;
    border-radius: 2px;
    outline: none;
    display: block;
    border: 1px solid #999999;
    padding: 8px 10px;
    font-size: 16px;
  }
  /deep/.area_box .van-cell {
    padding: 0;
  }
  /deep/.van-field__control {
    padding-top: 10px;
    font-size: 22px;
    color: rgba(80, 80, 80, 1);
  }
  .van-row .van-button {
    margin: 0 auto;
  }
  /deep/.van-dialog {
    width: 600px;
    height: 400px;
  }
  /deep/.van-dialog__content--isolated {
    min-height: 260px;
  }
  /deep/.van-dialog__message {
    font-size: 32px;
  }
  /deep/.van-dialog--round-button .van-dialog__cancel,
  /deep/.van-dialog--round-button .van-dialog__confirm {
    height: 80px;
  }
  /deep/.van-dialog--round-button .van-dialog__confirm {
    color: #fff;
    font-size: 30px;
  }
}
</style>
